package com.my.zl.console.service.autocode.impl;

import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.zl.common.dto.ReqDTO;
import com.my.zl.common.dto.advancedQuery.PageQueryDTO;
import com.my.zl.common.enums.resp.GlobalEnum;
import com.my.zl.common.exception.BizException;
import com.my.zl.common.utils.CopyUtil;
import com.my.zl.console.common.constant.AutocodeC;
import com.my.zl.console.dao.autocode.ZlWebComponentMapper;
import com.my.zl.console.dto.autocode.webComponent.ReqWebComponentAddDTO;
import com.my.zl.console.dto.autocode.webComponent.ReqWebComponentDelDTO;
import com.my.zl.console.dto.autocode.webComponent.ReqWebComponentDetailDTO;
import com.my.zl.console.dto.autocode.webComponent.ReqWebComponentEditDTO;
import com.my.zl.console.dto.autocode.webComponent.ReqWebComponentQueryDTO;
import com.my.zl.console.dto.autocode.webComponent.ReqWebComponentStateDTO;
import com.my.zl.console.dto.autocode.webComponent.ResWebComponentAddDTO;
import com.my.zl.console.dto.autocode.webComponent.ResWebComponentDelDTO;
import com.my.zl.console.dto.autocode.webComponent.ResWebComponentDetailDTO;
import com.my.zl.console.dto.autocode.webComponent.ResWebComponentEditDTO;
import com.my.zl.console.dto.autocode.webComponent.ResWebComponentQueryDTO;
import com.my.zl.console.dto.autocode.webComponent.ResWebComponentStateDTO;
import com.my.zl.console.dto.autocode.webComponent.ResWebComponentVO2;
import com.my.zl.console.entity.autocode.ZlWebComponent;
import com.my.zl.console.po.autocode.webComponent.ZlWebComponentVo;
import com.my.zl.console.service.autocode.IWebComponentService;
import com.my.zl.console.service.base.BaseManager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 前端组件
 * @author ZL
 */
@Service
public class WebComponentServiceImpl extends BaseManager implements IWebComponentService {

    @Autowired
    private ZlWebComponentMapper webComponentMapper;

    @Override
    public ResWebComponentQueryDTO queryWebComponentList(ReqWebComponentQueryDTO reqInfo,PageQueryDTO page) throws Exception{
        // TODO Auto-generated method stub
        ResWebComponentQueryDTO resDTO = BeanUtil.copyProperties(page, ResWebComponentQueryDTO.class);
        if(StringUtils.isNotBlank(reqInfo.getComponentTitle())){
            reqInfo.setComponentTitle("%"+reqInfo.getComponentTitle()+"%");
        }
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        
        pageHandle(page);//处理分页数据
        //当效率不行时自行计数，或完全自行分页，先查询总数，如果为0不用再次查询了；开启计数：true是,false否
        //临近第一条SQL查询语句自动分页,如果未消费时(即后面没有触发SQL查询)则需主动调用pageHp.clear()清理
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlWebComponentVo> list = webComponentMapper.selectListPage(paramMap);
        resDTO.setData(CopyUtil.copyList(list, ResWebComponentVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResWebComponentDetailDTO queryWebComponentDetail(ReqWebComponentDetailDTO reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ResWebComponentDetailDTO res = new ResWebComponentDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        ZlWebComponent view = webComponentMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResWebComponentAddDTO insertWebComponentInfo(ReqDTO<ReqWebComponentAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqWebComponentAddDTO reqData = reqInfo.getReqData();
        ResWebComponentAddDTO res = new ResWebComponentAddDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlWebComponent record = new ZlWebComponent();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setComponentId(IdUtil.fastSimpleUUID());
        record.setDataStatus(AutocodeC.DataStatus.ENABLE.getCode());//状态
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("componentId",record.getComponentId());
        //检查主键是否冲突
        if(webComponentMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        webComponentMapper.insertSelective(record);
        
        ZlWebComponent keyInfo = webComponentMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResWebComponentEditDTO updateWebComponentInfo(ReqDTO<ReqWebComponentEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqWebComponentEditDTO reqData = reqInfo.getReqData();
        ResWebComponentEditDTO res = new ResWebComponentEditDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlWebComponent record = new ZlWebComponent();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        webComponentMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("componentId",record.getComponentId());
        ZlWebComponent keyInfo = webComponentMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResWebComponentDelDTO deleteWebComponentInfo(ReqDTO<ReqWebComponentDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqWebComponentDelDTO reqData = reqInfo.getReqData();
        ResWebComponentDelDTO res = new ResWebComponentDelDTO();
        Instant time = getSystemTime().getUtcTime();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        webComponentMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResWebComponentStateDTO updateWebComponentState(ReqDTO<ReqWebComponentStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqWebComponentStateDTO reqData = reqInfo.getReqData();
        ResWebComponentStateDTO res = new ResWebComponentStateDTO();
        Instant time = getSystemTime().getUtcTime();
         
        ZlWebComponent record = new ZlWebComponent();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        webComponentMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("componentId",record.getComponentId());
        ZlWebComponent keyInfo = webComponentMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}